package group32.client;

import group32.shared.SearchResult;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
public class Search extends Composite{
	VerticalPanel hp = new VerticalPanel();
	VerticalPanel searchPanel = new VerticalPanel();
	HorizontalPanel mainPanel= new HorizontalPanel();
	HorizontalPanel search= new HorizontalPanel();
	HorizontalPanel h1= new HorizontalPanel();
		VerticalPanel v1 = new VerticalPanel();
		VerticalPanel v2 = new VerticalPanel();
		HorizontalPanel h2= new HorizontalPanel();
	final TextBox searchField = new TextBox();
	Button bSearch= new Button();
	Hyperlink logOff = new Hyperlink();
	Hyperlink friends = new Hyperlink();
	Hyperlink circles = new Hyperlink();
	Hyperlink photos = new Hyperlink();
	VerticalPanel sr= new VerticalPanel();
	Hyperlink userpage = new Hyperlink();
	Hyperlink more = new Hyperlink();
	Hyperlink more1 = new Hyperlink();
	String login;
	public int userID;
	int feedSize=15;
	private SearchServiceAsync sService = GWT
	.create(SearchService.class);
	@SuppressWarnings("deprecation")
	public Search(String keys, final int userID){

		userpage.setText("Home");
		hp.setHeight("700px");
			//hp.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);

				searchPanel.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);
				//searchPanel.setHeight("100px");
				searchPanel.setWidth("1050px");
				bSearch.setText("Search");
				logOff.setText("LogOff");
				logOff.setTargetHistoryToken("logout");
				//v1.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
				//v2.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
				v1.add(userpage);
				v2.add(logOff);
				h1.setSpacing(10);
				h2.setSpacing(2);
				h2.add(v1);h2.add(v2);
				h2.setHeight("40px");h2.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);
				circles.setText("Circles");
				circles.setTargetHistoryToken("circles");
				friends.setText("Friends");
				friends.setTargetHistoryToken("friends");
				photos.setText("Photos");
				photos.setTargetHistoryToken("photos");
				h1.add(userpage);h1.add(circles);h1.add(friends);h1.add(photos);
				searchField.setWidth("700px");
				search.setSpacing(10);
				search.add(searchField);search.add(bSearch);
				h1.add(search);
				h1.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);
				h1.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
				searchPanel.add(h2);
				searchPanel.add(h1);
				searchPanel.setStyleName(".paddedHorizontalbottom");
				sr.setSpacing(0);
				hp.add(searchPanel);
				searchPanel.add(sr);
				sr.setVisible(false);
				initWidget(hp);
		sService.search(keys, userID, new AsyncCallback<List<SearchResult>>() {
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
			
			public void onSuccess(List<SearchResult> result) {
				if(result!=null)searchDisplay(result);
				
			}
			});
		logOff.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	            FlowControl.go(new Login());             
	        }           
	    });
		userpage.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	            FlowControl.go(new HomePage(userID, Login.username));             
	        }           
	    });
		bSearch.addClickHandler(new ClickHandler() {
	        public void onClick(ClickEvent event) {
	        	String keys= searchField.getValue();
	        	if(keys.trim().length()>1){
	        		sService.search(keys, userID, new AsyncCallback<List<SearchResult>>() {
	        			public void onFailure(Throwable caught) {
	        				caught.printStackTrace();
	        			}
	        			public void onSuccess(List<SearchResult> result) {
	        				if(result!=null)searchDisplay(result);
	        			}
	        			});
	        	}
	   
	        	
	        }
	    });
	
	}
	
	@SuppressWarnings("deprecation")
	public void searchDisplay(List<SearchResult> results) {
		//List<String> results= new ArrayList<String>();
		sr.clear(); Hyperlink searchAnchorLabels[]= new Hyperlink[results.size()];
		HorizontalPanel searches[]= new HorizontalPanel[results.size()];
		int i=0; int y=0;
		for(SearchResult s:results){
			searches[i]= new HorizontalPanel();
			VerticalPanel photoPanel= new VerticalPanel();
			searches[i].setHeight("120px");
			searches[i].setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
			VerticalPanel infoPanel = new VerticalPanel();
			searchAnchorLabels[i]= new Hyperlink();
			searchAnchorLabels[i].setHTML("<img src='" +s.url+"'height=100px width=100px/>");
			
			final int a =s.photoid;
			photoPanel.add(searchAnchorLabels[i]);
			//if(s.photoOwner>=0)infoPanel.add(new Label("Photo Owner: "+s.photoOwner));
			//if(s.circleName!=null || s.circleName!="")infoPanel.add(new Label("Circle Name" +s.circleName));
			//System.out.println(s.score);
			searchAnchorLabels[i].addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
		           FlowControl.go(new Photo(userID,a,Login.username,1));             
		        }           
		    });
			searches[i].add(photoPanel);
			searches[i].add(infoPanel);
			sr.add(searches[i]);
			y++;
			if(y>feedSize){
			more.setText("more");
			sr.add(more);
			final int y1= y;
			final List<SearchResult> dummy= (List<SearchResult>) results;
			more.addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
		           showmore(y1,dummy) ;           
		        }           
		    });
			break;}
			
		}
		sr.setSpacing(10);
		sr.setVisible(true);
	}
	
	@SuppressWarnings("deprecation")
	public void showmore(int y, List<SearchResult> results){
		sr.remove(more);
		Hyperlink searchAnchorLabels[]= new Hyperlink[results.size()];
		HorizontalPanel searches[]= new HorizontalPanel[results.size()];
		int i=0;
		SearchResult[] sa = (SearchResult[])results.toArray();
		for(int j=y; j<y+feedSize;j++){
		SearchResult s= sa[j];
			searches[i]= new HorizontalPanel();
			VerticalPanel photoPanel= new VerticalPanel();
			searches[i].setHeight("120px");
			searches[i].setVerticalAlignment(VerticalPanel.ALIGN_MIDDLE);
			VerticalPanel infoPanel = new VerticalPanel();
			searchAnchorLabels[i]= new Hyperlink();
			searchAnchorLabels[i].setHTML("<img src='" +s.url+"'height=100px width=100px/>");
			
			final int a =s.photoid;
			photoPanel.add(searchAnchorLabels[i]);
			//if(s.photoOwner>=0)infoPanel.add(new Label("Photo Owner: "+s.photoOwner));
			//if(s.circleName!=null || s.circleName!="")infoPanel.add(new Label("Circle Name" +s.circleName));
			//System.out.println(s.score);
			searchAnchorLabels[i].addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
		           FlowControl.go(new Photo(userID,a,Login.username,1));             
		        }           
		    });
			searches[i].add(photoPanel);
			searches[i].add(infoPanel);
			sr.add(searches[i]);
			y++;
			if(y>feedSize){
			more1.setText("more");
			final int y1= y;
			final List<SearchResult> dummy= (List<SearchResult>) results;
			more1.addClickHandler(new ClickHandler() {
		        public void onClick(ClickEvent event) {
		           showmore(y1,dummy) ;           
		        }           
		    });
			break;}
			
		}
	}
	
}
